<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>模块管理</title>
		<script src="../js/global.js" type="text/javascript" charset="utf-8"></script>

	</head>

	<body>
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
			<legend>系統功能模块维护</legend>
		</fieldset>
		<div class="layui-container">
			<div class="layui-row">
				<div class="layui-col-md3">
					<div id="divModules" class="demo-tree demo-tree-box" style="width: 100%; height:100%; ">
					</div>
				</div>
				<div class="layui-col-md9">
					<div id="divModuleB">
						<button id="btnAddChild" type="button" class="layui-btn layui-btn-sm">添加子节点</button>
						<button id="btnChangeParent" type="button" class="layui-btn layui-btn-normal layui-btn-sm">设置父节点</button>
						<fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
							<legend><span id="spTitle">功能模块信息维护</span></legend>
						</fieldset>
						<form id="formSave" class="layui-form" action="">
							<div class="layui-form-item">
								<label class="layui-form-label">父节点编号</label>
								<div class="layui-input-block">
									<input id="txtParentId" type="text" disabled="disabled" value="0" class="layui-input">
									<input id="hfParentId" type="hidden" name="parentId">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">节点编号</label>
								<div class="layui-input-block">
									<input id="hfId" type="hidden" name="mid">
									<input id="txtId" type="text" disabled="disabled" value="0" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">节点名称</label>
								<div class="layui-input-block">
									<input id="txtName" type="text" name="mname" lay-verify="required" autocomplete="off" placeholder="请输入节点名称" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">路径</label>
								<div class="layui-input-block">
									<input id="txtPath" type="text" name="mpath" placeholder="请输入功能路径" autocomplete="off" class="layui-input">
								</div>
							</div>

							<div class="layui-form-item">
								<label class="layui-form-label">权重</label>
								<div class="layui-input-block">
									<input id="txtWeight" type="text" name="mweight" lay-verify="required|number" placeholder="请输入权重(整数)" autocomplete="off" class="layui-input">
								</div>
							</div>

							<div class="layui-form-item">
								<div class="layui-input-block">
									<button class="layui-btn" lay-submit="" lay-filter="subData">保存</button>
									<button type="reset" class="layui-btn layui-btn-primary">重置</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
		<script>
			var treeMethod = {
				add: function(obj) {

				},
				update: function(obj) {

				},
				del: function(obj) {

				}
			}
		</script>

		<script>
			var treeNode = null;
			var tree, layer, $, form;
			layui.use(['tree', 'layer', 'form'], function() {
				tree = layui.tree;
				layer = layui.layer;
				$ = layui.$;
				form = layui.form;
				form.on('submit(subData)', function(data) {
					var ix = layer.load(0, {
						shade: false
					});
					//添加子节点操作
					if($("#hfId").val() == "0") {
						var str = $("#formSave").serialize() + "&token=" + globalData.myToken;
						$.post(globalData.server + "CreateModule", str, function(res) {
							if(res.code==0) {
								layer.msg("添加成功");
								active["reload"].call(null);
							} else
								layer.msg(res.msg);
							layer.close(ix);
						}, "json");
					} else { //更新节点或调整父节点
						$("#hfParentId").val($("#txtParentId").val());
						var title = $("#txtName").val();
						var str = $("#formSave").serialize() + "&token=" + globalData.myToken;
						$.post(globalData.server + "UpdateModule", str, function(res) {
							if(res.code==0) {
								layer.msg("保存成功");
								$("#txtParentId").attr("disabled", "disabled");
								$("#txtId").attr("disabled", "disabled");
								treeNode.data.title = title;
								treeNode.elem.find('.layui-tree-txt').html(title);
							} else
								layer.msg(res.msg);
							layer.close(ix);
						}, "json");
					}
					return false;
				});
				$(function() {
					$("#btnAddChild").click(function() {
						var id = $("#hfId").val();
						var name = $("#txtName").val();
						$("#spTitle").html("为【" + name + "】添加子节点");
						$("#hfId").val("0");
						$("#txtId").val("0");
						$("#hfParentId").val(id);
						$("#txtParentId").val(id);
						$("#txtName").val("");
						$("#txtPath").val("");
						$("#txtWeight").val("");
					});
					$("#btnChangeParent").click(function() {
						var id = $("#hfId").val();
						var name = $("#txtName").val();
						$("#spTitle").html("为【" + name + "】调整父编号");
						$("#txtParentId").removeAttr("disabled");
					});
				})
				active = {
					reload: function() {
						$.post(globalData.server + "GetAllModuleLayUI", {
							"token": globalData.myToken
						}, function(res) {
							tree.reload('tv', {
								data: res
							});
						}, "json");
					},
					load: function() { //加载当前页
						$.post(globalData.server + "GetAllModuleLayUI", {
							"token": globalData.myToken
						}, function(res) {
							tree.render({
								id: "tv",
								elem: "#divModules",
								data: res,
								edit: ['del'],

								operate: function(obj) {
									var type = obj.type; //得到操作类型：add、edit、del
									var data = obj.data; //得到当前节点的数据
									var elem = obj.elem; //得到当前节点元素

									//Ajax 操作
									var id = data.id; //得到节点索引
									if(type === 'del') { //增加节点
										$.post(globalData.server + "DeleteModule", {
											token: globalData.myToken,
											mid: id
										}, function(res) {
											if(res.code==0) {
												obj.del();
												layer.msg("删除成功");
											} else {
												layer.msg(res.msg);
											}
										}, "json");
									}
								},
								click: function(obj) {
									$("#spTitle").html("功能模块信息维护");
									treeNode = obj;
									var index = layer.load(0, {
										shade: false
									});
									$.post(globalData.server + "GetModuleById", {
										"token": globalData.myToken,
										"mid": obj.data.id
									}, function(res) {
										if(res.code==0) {
											//var d = JSON.parse(res.msg);
											$("#txtId").val(res.data.mid)
											$("#hfId").val(res.data.mid);
											$("#txtParentId").val(res.data.parentId);
											$("#hfParentId").val(res.data.parentId);
											$("#txtName").val(res.data.mname);
											$("#txtPath").val(res.data.mpath);
											$("#txtWeight").val(res.data.mweight);
										} else {
											layer.msg(res.msg);
										}
										layer.close(index);
									}, "json")
								}
							});
						}, "json");
					}
				};
				active["load"].call(null);
			});
		</script>

	</body>

</html>